//
//  ViewController.m
//  BYPermanentThread
//
//  Created by 崔宝印 on 2018/8/7.
//  Copyright © 2018年 Koolearn. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSThread *byThread;
@property (nonatomic, assign) BOOL stop;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _stop = NO;
    [self testThread];
    [self performSelector:@selector(test) onThread:self.byThread withObject:nil waitUntilDone:YES];
}

- (void)testThread
{
    __weak typeof(self) weakSelf = self;
    self.byThread = [[NSThread alloc] initWithBlock:^{
        NSLog(@"%@_run",[NSThread currentThread]);
        [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
        while (!weakSelf.stop)
        {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            NSLog(@"%@_end",[NSThread currentThread]);
        }
    }];
    [self.byThread start];
}

- (void)test
{
    NSLog(@"%s",__func__);
}

- (void)exit
{
    NSLog(@"%s",__func__);
    self.stop = YES;
//    [NSThread exit];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(exit) onThread:self.byThread withObject:nil waitUntilDone:YES];
}
@end
